package com.sskj.b70.v.activity;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.sskj.b70.MyAppLication;
import com.sskj.b70.R;
import com.sskj.b70.common.App;
import com.sskj.b70.common.Constants;
import com.sskj.b70.common.widget.ToolBarLayout;
import com.sskj.b70.p.activity.CashPresenter;
import com.sskj.b70.utils.ClickUtil;
import com.sskj.b70.utils.PermissionTipUtil;
import com.sskj.b70.utils.ToastUtil;
import com.sskj.b70.v.base.BaseActivity;
import com.tbruyelle.rxpermissions2.RxPermissions;

import java.util.HashMap;

import butterknife.BindView;

/**
 * <pre>
 *     author : 李岩
 *     e-mail : 465357793@qq.com
 *     time   : 2018/04/03
 *     desc   : 添加提现地址
 *     version: 1.0
 * </pre>
 */
public class CashAddressAddActivity extends BaseActivity<CashPresenter> {

    @BindView(R.id.cash_addreess_add_submit)
    Button mSubmit;
    @BindView(R.id.cash_addreess_add_input)
    EditText mAddressInput;
    @BindView(R.id.cash_addreess_add_remark)
    EditText mAddressRemarkInput;
    //    @BindView(R.id.toolBar)
//    ToolBarLayout mToolBar;
    @BindView(R.id.toolBar)
    ToolBarLayout mToolBar;
    @BindView(R.id.qr_scan)
    ImageView MIvQRScan;
    @BindView(R.id.title)
    TextView title;

    private TextView.OnEditorActionListener dealEditorAction = (v, actionId, event) -> {
        //以下方法防止两次发送请求
        if (actionId == EditorInfo.IME_ACTION_SEND ||
                (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
            switch (event.getAction()) {
                case KeyEvent.ACTION_UP://"你点了软键盘'回车'按钮"
                    //						//隐藏软键盘
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
                    return true;
                default:
                    return true;
            }
        }
        return false;
    };
    private String mAddressType = "";
    private HashMap<String, String> typeMap;
    private HashMap<String, String> codeMap;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_cash_address_add;
    }

    @Override
    public CashPresenter getPresenter() {
        return new CashPresenter();
    }

    @Override
    protected void onViewBinding() {
        typeMap = new HashMap<>();
        codeMap = new HashMap<>();
        codeMap.put(App.INSTANCE.getString(R.string.strCashAddressAddActivity2), "BTC");
        codeMap.put(App.INSTANCE.getString(R.string.strCashAddressAddActivity22), "BCH");
        codeMap.put(App.INSTANCE.getString(R.string.strCashAddressAddActivity4), "ETH");
        codeMap.put(App.INSTANCE.getString(R.string.strCashAddressAddActivity23), "USDT");
        typeMap.put(App.INSTANCE.getString(R.string.strCashAddressAddActivity2), "2");
        typeMap.put(App.INSTANCE.getString(R.string.strCashAddressAddActivity22), "3");
        typeMap.put(App.INSTANCE.getString(R.string.strCashAddressAddActivity4), "1");
        typeMap.put(App.INSTANCE.getString(R.string.strCashAddressAddActivity23), "2");
        mAddressType = getIntent().getStringExtra(Constants.INTENT_ADDRESS_TYPE);
        mAddressRemarkInput.setOnEditorActionListener(dealEditorAction);
        mToolBar.setLeftButtonOnClickLinster(v -> finish());
//        mToolBar.setTitle(mAddressType.toUpperCase() + App.INSTANCE.getString(R.string.strCashAddressAddActivity6));

        title.setText(mAddressType.toUpperCase() + App.INSTANCE.getString(R.string.strCashAddressAddActivity6));
        ClickUtil.click(MIvQRScan, () -> {
            new RxPermissions(CashAddressAddActivity.this)
                    .request(Manifest.permission.CAMERA)
                    .subscribe(isGrant -> {
                        if (isGrant) {
                            startActivityForResult(new Intent(mActivity, QuickMarkActivity.class), Constants.SACN_QR);
                        } else {
                            PermissionTipUtil.tip(CashAddressAddActivity.this, App.INSTANCE.getString(R.string.strCashAddressAddActivity8));
                        }
                    });
        });
        ClickUtil.click(mSubmit, () -> {
            String address = mAddressInput.getText().toString().trim();
            if (TextUtils.isEmpty(address)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.strCashAddressAddActivity9));
            } else {
                mPresenter.requestAddAddress(address, mAddressRemarkInput.getText().toString().trim(), MyAppLication.getMobile(), typeMap.get(mAddressType),codeMap.get(mAddressType));
            }
        });
    }


    public void onResponseCashAddressAddSuccess() {
        finish();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == Constants.SACN_QR) {
            String scan_result = data.getStringExtra("scan_result");
            mAddressInput.setText(TextUtils.isEmpty(scan_result) ? "" : scan_result);
        }
    }
}
